package nbcp.base.config

import org.slf4j.LoggerFactory
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.beans.factory.annotation.Value
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
import nbcp.base.utils.SpringUtil
import nbcp.web.config.MyAllFilter
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
//import org.springframework.session.web.http.DefaultCookieSerializer
//import org.springframework.messaging.converter.StringMessageConverter
import java.util.ArrayList


/**
 * Created by udi on 2017.3.11.
 */

@Configuration
open class MyWebMvcConfig : WebMvcConfigurationSupport() {
    companion object {
        private val logger = LoggerFactory.getLogger(this::class.java.declaringClass)
    }
//    @Bean
//    open fun abc(): def {
//        println("abc")
//        return def();
//    }
//
//    class def : RequestMappingHandlerAdapter() {
//        override fun afterPropertiesSet() {
//            super.afterPropertiesSet()
//
//            //反射得到 argumentResolvers HandlerMethodArgumentResolverComposite
//
//            var adpterField_Resolvers = this::class.java.superclass.getDeclaredField("argumentResolvers");
//            adpterField_Resolvers.isAccessible = true;
//
//            var adpterField_Resolvers_Value0 = adpterField_Resolvers.get(this);
//            if (adpterField_Resolvers_Value0 == null) return;
//
//            var adpterField_Resolvers_Value = adpterField_Resolvers_Value0 as HandlerMethodArgumentResolverComposite;
//
//            var compositeField_Resolvers = adpterField_Resolvers_Value::class.java.getDeclaredField("argumentResolvers")
//            compositeField_Resolvers.isAccessible = true;
//
//            var compositeField_Resolvers_Value0 = compositeField_Resolvers.get(adpterField_Resolvers_Value);
//
//            if (compositeField_Resolvers_Value0 == null) return;
//
//            var compositeField_Resolvers_Value = compositeField_Resolvers_Value0 as LinkedList<HandlerMethodArgumentResolver>;
//
//            compositeField_Resolvers_Value.forEach {
//                println(it::class.java.name)
//            }
//        }
//    }

    @Value("\${server.servlet.session.cookie.name:}")
    var SessionKey: String = "";

//    @Bean
//    fun defaultCookieSerializer() : DefaultCookieSerializer {
//        var ret = DefaultCookieSerializer();
//        ret.setCookieName(SessionKey)  ;
//        return ret;
//    }



//    @Autowired
//    lateinit var filter :MyAllFilter
//
//    @Bean
//    fun filterRegistrationBean(): FilterRegistrationBean<MyAllFilter> {
//        val registrationBean = FilterRegistrationBean<MyAllFilter>()
//        registrationBean.order = 10
//        registrationBean.filter = filter
//        val urlPatterns = ArrayList<String>()
//        urlPatterns.add("/*")
//        urlPatterns.add("/**")
//        registrationBean.urlPatterns = urlPatterns
//        return registrationBean
//    }

    @Autowired
    lateinit var myInterceptor: MyInterceptor

    override fun addInterceptors(registry: InterceptorRegistry) {
        registry.addInterceptor(myInterceptor)
                .order(-5)
                .addPathPatterns(  "/*"  )
                .addPathPatterns(  "/**"  )
    }


//    @Bean
//    fun RegisteAuthImageServlet(): ServletRegistrationBean<AuthImageServlet> {
//        var registration = ServletRegistrationBean(AuthImageServlet())
//        registration.isEnabled = true
//        registration.addUrlMappings("/open/getValidateCodeImage")
//        return registration
//    }

//    class ObjectIdSerializer : ObjectSerializer {
//        override fun write(serializer: JSONSerializer?, value: Any?, filedName: Any?, fieldType: Type?, features: Int) {
//            val out = serializer!!.getWriter()
//            if (value == null) {
//                out.writeNull()
//                return
//            }
//
//
//            out.writeString(value.toString())
//        }
//    }



}